package com.sptpc.fileio;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    Button btn1;
    Button btn2;
    TextView tv;
    EditText editText;
    private static String [] PERMISSION = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.MOUNT_FORMAT_FILESYSTEMS,
            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        checkPermission();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        btn1 = findViewById(R.id.Save);
        btn2 = findViewById(R.id.Read);
        editText = findViewById(R.id.et);
        File path = Environment.getExternalStorageDirectory();
        final String rootPath = path.getAbsolutePath() + "/1812212246";

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FileReader filedir = null;
                    filedir = new FileReader(rootPath+"/123.txt");
                    BufferedReader br = new BufferedReader(filedir);
                    String s = null;
                    int i = 0;
                    while ((s = br.readLine())!= null){
                        tv.setText(s);
                    }
                    br.close();;
                    filedir.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                File filedir = new File(rootPath);
                if (!filedir.exists())  filedir.mkdir();

                        File file = new File(rootPath,"123.txt");

                            if (!file.exists()) {
                                try {
                                    file.createNewFile();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                    FileWriter fw = null;
                    try {
                        fw = new FileWriter(file,true);
                        String content = editText.getText().toString();
                        fw.write(content);
                        fw.flush();
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

//                try {
//                    File file = new File(path,"test.txt");
//                    FileWriter fileWriter = new FileWriter(file,true);
//                    fileWriter.write("Test write");
//                    fileWriter.flush();
//                    fileWriter.close();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
        });
    }
    private void checkPermission()
    {
        int permission = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this,PERMISSION,100);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100)
        {

            if (grantResults[0] == PackageManager.PERMISSION_DENIED ||
                grantResults[1] == PackageManager.PERMISSION_DENIED)
                    finish();
        }
    }
}
